package johnkkk.com.gl3x;

import android.opengl.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.FrameLayout;

import com.code.AbsGLRender;
import com.code.SimpleGLRender;
import com.graphics.GLGraphics;
import com.graphics.GLRender;
import com.input.GameAction;
import com.kongqw.rockerlibrary.view.RockerView;
import com.math3d.MatrixUtil;
import com.system.Camera;
import com.ui.FullActivity;

import johnkkk.com.test.BulletTestGLRender;
import johnkkk.com.test.ObjGLRender;

/**
 * Created by JhonKkk on 2018/11/18.
 */

public class BulletTestActivity extends FullActivity {
    //    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_object_test);
//        findViewById(R.id.bt_load_obj).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ObjectLoader objectLoader = new ObjectLoader();
//                objectLoader.loadObject("obj/wrraing.obj");
////                ObjectGLConvertTools.convertObjectModel(objectLoader);
//                List<ObjectLoader.ObjectGroup> groups =  objectLoader.getGroupArray();
//                for(ObjectLoader.ObjectGroup group : groups){
//                    GLLog.loge(group.toString());
//                }
//            }
//        });
//    }
    private RockerView mRockerView;
    private Camera mCamera;
    private GameAction mUpAction;
    private GameAction mDownAction;
    private GameAction mLeftAction;
    private GameAction mRightAction;

    private GameAction mMouseUpAction;
    private GameAction mMouseDownAction;
    private GameAction mMouseLeftAction;
    private GameAction mMouseRightAction;
    private AbsGLRender mAbsGLRender;

    private void initInput(){
        mUpAction = new GameAction("up");
        mDownAction = new GameAction("down");
        mLeftAction = new GameAction("left");
        mRightAction = new GameAction("right");


        mMouseUpAction = new GameAction("mup");
        mMouseDownAction = new GameAction("mdown");
        mMouseLeftAction = new GameAction("mleft");
        mMouseRightAction = new GameAction("mright");
    }
    private class IGLooper implements GLRender.IGLLooper{
        private float detaX, detaY;
        private float radinXAxis, radinYAxis;

        @Override
        public void init() {
            mCamera = mAbsGLRender.mCamera;
            mCamera.eye.getVec()[0] = 0;
            mCamera.eye.getVec()[1] = 0;
            mCamera.eye.getVec()[2] = 0;
        }

        /**
         * 处理输入控制
         * @param exTime
         */
        private final void handlerInput(double exTime){
            float sp_v = (float) (0.05f * exTime);
            float rate = (float) (0.03f * exTime);
            mCamera = mAbsGLRender.mCamera;
            boolean isRotate = false;
            //处理右摇杆
            if(mMouseUpAction.isPressed()){
                detaY += rate;
                isRotate = true;
            }
            else if(mMouseDownAction.isPressed()){
                detaY -= rate;
                isRotate = true;
            }
            if(mMouseLeftAction.isPressed()){
                detaX += rate;
                isRotate = true;
            }
            else if(mMouseRightAction.isPressed()){
                detaX -= rate;
                isRotate = true;
            }
            if(isRotate){
                radinXAxis = (float) Math.toRadians(detaY);
                radinYAxis = (float) Math.toRadians(detaX);
                MatrixUtil.normalize(mCamera.mCameraRotateMat4);
                Matrix.rotateM(mCamera.mCameraRotateMat4, 0, detaX, 0.0f, 1.0f, 0.0f);
                Matrix.rotateM(mCamera.mCameraRotateMat4, 0, detaY, 1.0f, 0.0f, 0.0f);
            }
            //处理左摇杆
            if(mUpAction.isPressed()){
                mCamera.eye.getVec()[2] -= Math.cos(radinYAxis) * sp_v;
                mCamera.eye.getVec()[0] -= Math.sin(radinYAxis) * sp_v;
                mCamera.eye.getVec()[1] += Math.sin(radinXAxis) * sp_v;
            }
            else if(mDownAction.isPressed()){
                mCamera.eye.getVec()[2] += Math.cos(radinYAxis) * sp_v;
                mCamera.eye.getVec()[0] += Math.sin(radinYAxis) * sp_v;
                mCamera.eye.getVec()[1] -= Math.sin(radinXAxis) * sp_v;
            }
            if(mLeftAction.isPressed()){
                mCamera.eye.getVec()[2] += Math.sin(radinYAxis) * sp_v;
                mCamera.eye.getVec()[0] -= Math.cos(radinYAxis) * sp_v;
            }
            else if(mRightAction.isPressed()){
                mCamera.eye.getVec()[2] -= Math.sin(radinYAxis) * sp_v;
                mCamera.eye.getVec()[0] += Math.cos(radinYAxis) * sp_v;
            }
        }
        @Override
        public void glLooper(double exTime) {
            handlerInput(exTime);
        }
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAbsGLRender = new BulletTestGLRender(this);
        mAbsGLRender.setGLooper(new BulletTestActivity.IGLooper());
        setContentView(R.layout.activity_game);
        GLGraphics.init(this, 3, mAbsGLRender);
        initInput();
        FrameLayout fl = findViewById(R.id.activity_game_fl);
        fl.addView(GLGraphics.getGLSurfaceView());

        mRockerView = findViewById(R.id.rockerView);
        mRockerView.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_MOVE);
        mRockerView.setOnShakeListener(RockerView.DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
            @Override
            public void onStart() {
                //按下
            }

            @Override
            public void direction(RockerView.Direction direction) {
                switch (direction){
                    case DIRECTION_LEFT:
                        mRightAction.reset();
                        mUpAction.reset();
                        mDownAction.reset();

                        mLeftAction.press();
                        break;
                    case DIRECTION_RIGHT:
                        mLeftAction.reset();
                        mUpAction.reset();
                        mDownAction.reset();

                        mRightAction.press();
                        break;
                    case DIRECTION_UP:
                        mLeftAction.reset();
                        mRightAction.reset();
                        mDownAction.reset();

                        mUpAction.press();
                        break;
                    case DIRECTION_DOWN:
                        mLeftAction.reset();
                        mRightAction.reset();
                        mUpAction.reset();

                        mDownAction.press();
                        break;
                    case DIRECTION_UP_LEFT:
                        onFinish();
                        mRightAction.reset();
                        mUpAction.reset();
                        mDownAction.reset();

                        mLeftAction.press();
                        break;
                    case DIRECTION_UP_RIGHT:
                        onFinish();
                        mUpAction.press();
                        mRightAction.press();
                        break;
                    case DIRECTION_DOWN_LEFT:
                        onFinish();
                        mDownAction.press();
                        mLeftAction.press();
                        break;
                    case DIRECTION_DOWN_RIGHT:
                        onFinish();
                        mRightAction.press();
                        mDownAction.press();
                        break;
                }
            }

            @Override
            public void onFinish() {
                //抬起
                mLeftAction.reset();
                mRightAction.reset();
                mUpAction.reset();
                mDownAction.reset();
            }
        });



        mRockerView = findViewById(R.id.rockerView_right);
        mRockerView.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_MOVE);
        mRockerView.setOnShakeListener(RockerView.DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
            @Override
            public void onStart() {
                //按下
            }

            @Override
            public void direction(RockerView.Direction direction) {
                switch (direction){
                    case DIRECTION_LEFT:
                        mMouseRightAction.reset();
                        mMouseUpAction.reset();
                        mMouseDownAction.reset();

                        mMouseLeftAction.press();
                        break;
                    case DIRECTION_RIGHT:
                        mMouseLeftAction.reset();
                        mMouseUpAction.reset();
                        mMouseDownAction.reset();

                        mMouseRightAction.press();
                        break;
                    case DIRECTION_UP:
                        mMouseLeftAction.reset();
                        mMouseRightAction.reset();
                        mMouseDownAction.reset();

                        mMouseUpAction.press();
                        break;
                    case DIRECTION_DOWN:
                        mMouseLeftAction.reset();
                        mMouseRightAction.reset();
                        mMouseUpAction.reset();

                        mMouseDownAction.press();
                        break;
                    case DIRECTION_UP_LEFT:
                        onFinish();
                        mMouseUpAction.press();
                        mMouseLeftAction.press();
                        break;
                    case DIRECTION_UP_RIGHT:
                        onFinish();
                        mMouseUpAction.press();
                        mMouseRightAction.press();
                        break;
                    case DIRECTION_DOWN_LEFT:
                        onFinish();
                        mMouseDownAction.press();
                        mMouseLeftAction.press();
                        break;
                    case DIRECTION_DOWN_RIGHT:
                        onFinish();
                        mMouseRightAction.press();
                        mMouseDownAction.press();
                        break;
                }
            }

            @Override
            public void onFinish() {
                //抬起
                mMouseLeftAction.reset();
                mMouseRightAction.reset();
                mMouseUpAction.reset();
                mMouseDownAction.reset();
            }
        });
    }
}
